//搜索框
var t = null
function searchBook() {

    const searchInput = document.querySelector('.searchinput')
    const searchUl = document.querySelector('.searchul')
    const aoa = document.querySelector('.aoa')
    const loading = document.querySelector('.loading');

    aoa.innerHTML = `
        <div class="seatop">
            <div class="searchtitle">
            </div>
            <p>搜“${searchInput.value}”相关作品>></p>
        </div>
    `
    if (!searchInput.value.trim()) {
        searchUl.innerHTML = ''
        aoa.innerHTML = ''
        loading.style.display = 'none';


    }
    if (t) {
        clearTimeout(t)
    }
    t = setTimeout(function () {
        if (!searchInput.value) {
            loading.style.display = 'none';
            return;
        } else {
            loading.style.display = 'block';
        }

        fetch('https://app2.motie.com/search?objectType=2&word=' + searchInput.value + '&pageSize=2').then(res => res.json()).then(res1 => {
            // console.log(res1.data.bookList)

            searchUl.innerHTML = ''
            loading.style.display = 'none';
            res1.data.bookList.forEach(item => {
                searchUl.innerHTML += `
                    <li class="searchli">
                        <img src=${item.authorIcon} alt="">
                        <div class="seatex">
                            <p class="p1">${item.name}</p>
                            <p>${item.authorName}</p>
                        </div>
                    </li>

                `

            })

        }).catch(err => {
            console.log(err)  //错误信息捕获
        })
    }, 500)


}


//返回顶部按钮
// 获取“返回顶部”按钮
const backToTopBtn = document.getElementById('backToTop');
// 监听页面滚动事件：滚动一定距离后，显示“返回顶部”按钮
window.addEventListener('scroll', () => {
    // 当滚动距离大于 300px 时，显示按钮；否则隐藏
    if (window.scrollY > 300) {
        backToTopBtn.style.display = 'block';
    } else {
        backToTopBtn.style.display = 'none';
    }
});
// 监听按钮点击事件：点击后，页面平滑滚动到顶部
backToTopBtn.addEventListener('click', () => {
    // 使用 window.scrollTo 实现平滑滚动
    window.scrollTo({
        top: 0,             // 滚动到顶部（top: 0）
        behavior: 'smooth'  // 平滑滚动效果
    });
});

